import axios from 'axios';
import { message } from 'antd';
import { getToken } from './storage';
import history from './history';
const request = axios.create({
  baseURL: 'http://geek.itheima.net',
  timeout: 5000,
});
request.interceptors.request.use(
  (config) => {
    if (getToken()) {
      config.headers.Authorization = `Bearer ${getToken()}`;
    }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);
request.interceptors.response.use(
  (res) => {
    return res.data;
  },
  async (err) => {
    // console.dir(err);
    // console.log(new Error(err));
    if (err.response.status === 401) {
      await history.push('/login');
    }
    if (err.response) {
      message.warning(err.response.data.message, 1);
    }
    return Promise.reject(err);
  }
);
export default request;
